package com.diandian.backend.store.service;

import com.diandian.backend.store.constants.DianDianStoreConstant;
import com.diandian.backend.store.domain.StoreOfflineStoreInfo;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.OfflineStoreMapper;
import com.diandian.backend.store.resp.*;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class OfflineStoreService {

    @Autowired
    private OfflineStoreMapper offlineStoreMapper;

    @Autowired
    private StoreOfflineStoreInfoService storeOfflineStoreInfoService;

    @Autowired
    private StoreOfflineStoreTreeService storeOfflineStoreTreeService;

    @Autowired
    private StoreOfflineStoreInfoTreeService storeOfflineStoreInfoTreeService;

    public PageResult storePage(StoreOfflineStorePageParamDTO dataDTO){

        dataDTO.check();
        int pageNum = dataDTO.getPageNum();
        int pageSize = dataDTO.getPageSize();
        String nodeCode = dataDTO.getNodeCodeEql();

        // 校验节点信息
        String nodePath = "";
        // 非顶级节点、非未分组节点
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode) && !DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)){
            StoreOfflineStoreTreeDetailParamDTO treeDetailParamDTO = new StoreOfflineStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(dataDTO.getNodeCodeEql());
            treeDetailParamDTO.setBelong(dataDTO.getBelongEql());
            treeDetailParamDTO.setOrgId(dataDTO.getOrgIdEql());
            treeDetailParamDTO.setOrgCode(dataDTO.getOrgCodeEql());
            treeDetailParamDTO.setDel(0);
            StoreOfflineStoreTreeDetailResp treeDetailResp = storeOfflineStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("当前节点不合法");
            }
            nodePath = treeDetailResp.getNodePath();
        }
        // 未分组节点
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)) {
            nodePath = DianDianStoreConstant.TREE_NODE_CODE_UNGROUP;
        }

        // 分页查询所有当前节点下的门店数据
        int totalCount = offlineStoreMapper.getStoreCount(nodePath, dataDTO);
        List<StoreOfflineStoreInfo> dataList = offlineStoreMapper.getStorePage(nodePath, dataDTO, pageSize, (pageNum - 1) * pageSize);

        // 组装数据
        List<StoreOfflineStoreInfoPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){

            StoreOfflineStoreInfoTreeListParamDTO listParamDTO = new StoreOfflineStoreInfoTreeListParamDTO();
            listParamDTO.setBelongEql(dataDTO.getBelongEql());
            listParamDTO.setOrgIdEql(dataDTO.getOrgIdEql());
            listParamDTO.setOrgCodeEql(dataDTO.getOrgCodeEql());

            List<StoreOfflineStoreInfoTreeListResp> relationList = storeOfflineStoreInfoTreeService.getList(listParamDTO);
            Map<String, String> storeCodeAndNodeCodeMap = new HashMap<>();
            if(relationList != null && relationList.size() > 0){
                for (int i = 0; i < relationList.size(); i++) {
                    storeCodeAndNodeCodeMap.put(relationList.get(i).getStoreCode(), relationList.get(i).getNodeCode());
                }
            }

            StoreOfflineStoreTreeListParamDTO treeListParamDTO = new StoreOfflineStoreTreeListParamDTO();
            treeListParamDTO.setBelongEql(dataDTO.getBelongEql());
            treeListParamDTO.setOrgIdEql(dataDTO.getOrgIdEql());
            treeListParamDTO.setOrgCodeEql(dataDTO.getOrgCodeEql());
            List<StoreOfflineStoreTreeListResp> treeListRespList = storeOfflineStoreTreeService.getList(treeListParamDTO);
            Map<String, String> treeCodeAndNameMap = new HashMap<>();
            if(treeListRespList != null && treeListRespList.size() > 0){
                for (int i = 0; i < treeListRespList.size(); i++) {
                    treeCodeAndNameMap.put(treeListRespList.get(i).getNodeCode(), treeListRespList.get(i).getNodeName());
                }
            }

            dataList.stream().forEach( x ->{
                StoreOfflineStoreInfoPageResp item = new StoreOfflineStoreInfoPageResp();
                item.setId(x.getId());
                item.setCode(x.getCode());
                item.setName(x.getName());
                item.setLogo(x.getLogo());
                item.setContactUserName(x.getContactUserName());
                item.setContactTel(x.getContactTel());
                item.setAddress(x.getAddress());
                item.setCoordinate(x.getCoordinate());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                if(storeCodeAndNodeCodeMap.get(x.getCode()) == null){
                    item.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
                    item.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_UNGROUP);
                } else {
                    String code = storeCodeAndNodeCodeMap.get(x.getCode());
                    item.setNodeCode(code);
                    item.setNodeName(treeCodeAndNameMap.get(code));
                }
                respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public StoreOfflineStoreInfoDetailResp getStoreDetail(OfflineStoreDetailParamDTO param) {

        String belong = param.getBelongEql();

        StoreOfflineStoreInfoDetailParamDTO storeInfoDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeInfoDetailParamDTO.setId(param.getId());
        storeInfoDetailParamDTO.setCode(param.getCode());
        storeInfoDetailParamDTO.setBelong(belong);
        storeInfoDetailParamDTO.setOrgId(param.getOrgIdEql());
        storeInfoDetailParamDTO.setOrgCode(param.getOrgCodeEql());
        storeInfoDetailParamDTO.setDel(0);

        StoreOfflineStoreInfoDetailResp detailResp = storeOfflineStoreInfoService.getDetail(storeInfoDetailParamDTO);
        if(detailResp == null){
            return null;
        }
        String storeCode = detailResp.getCode();
        String orgId = detailResp.getOrgId();
        String orgCode = detailResp.getOrgCode();

        StoreOfflineStoreInfoTreeDetailResp relationDTO = storeOfflineStoreInfoTreeService.getNodeCodeByStoreCode(belong, orgId, orgCode, storeCode);
        detailResp.setNodeCode((relationDTO == null || relationDTO.getNodeCode() == null) ? "" : relationDTO.getNodeCode());
        return detailResp;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addStore(OfflineStoreAddParamDTO dataDTO){

        String belong = dataDTO.getBelong();
        String nodeCode = dataDTO.getNodeCode();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String storeCode = dataDTO.getCode();
        String storeName = dataDTO.getName();

        // 校验节点是否合法：如果是未分组节点则设置为空
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode) && !DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)){
            StoreOfflineStoreTreeDetailParamDTO treeDetailParamDTO = new StoreOfflineStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(nodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreOfflineStoreTreeDetailResp treeDetailResp = storeOfflineStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("当前节点不合法");
            }
        } else if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)) {
            nodeCode = null;
        }

        // 校验门店编码和名称是否存在
        StoreOfflineStoreInfoDetailParamDTO storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreOfflineStoreInfoDetailResp storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp != null){
            throw new DefaultException("已存在此门店编码");
        }

//        storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
//        storeDetailParamDTO.setBelong(belong);
//        storeDetailParamDTO.setOrgId(orgId);
//        storeDetailParamDTO.setOrgCode(orgCode);
//        storeDetailParamDTO.setName(storeName);
//        storeDetailParamDTO.setDel(0);
//        storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
//        if(storeInfoDetailResp != null){
//            throw new DefaultException("已存在此门店名称");
//        }

        // 保存门店信息
        StoreOfflineStoreInfoAddParamDTO storeInfoAddParamDTO = new StoreOfflineStoreInfoAddParamDTO();
        storeInfoAddParamDTO.setCode(storeCode);
        storeInfoAddParamDTO.setName(storeName);
        storeInfoAddParamDTO.setLogo(dataDTO.getLogo());
        storeInfoAddParamDTO.setContactUserName(dataDTO.getContactUserName());
        storeInfoAddParamDTO.setContactTel(dataDTO.getContactTel());
        storeInfoAddParamDTO.setAddress(dataDTO.getAddress());
        storeInfoAddParamDTO.setCoordinate(dataDTO.getCoordinate());
        storeInfoAddParamDTO.setRemark(dataDTO.getRemark());
        storeInfoAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
        storeInfoAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
        storeInfoAddParamDTO.setOrgId(orgId);
        storeInfoAddParamDTO.setOrgCode(orgCode);
        storeInfoAddParamDTO.setBelong(belong);
        storeOfflineStoreInfoService.add(storeInfoAddParamDTO);

        // 保存门店和节点的关联关系
        if(nodeCode != null){
            StoreOfflineStoreInfoTreeAddParamDTO storeInfoTreeAddParamDTO = new StoreOfflineStoreInfoTreeAddParamDTO();
            storeInfoTreeAddParamDTO.setNodeCode(nodeCode);
            storeInfoTreeAddParamDTO.setStoreCode(storeCode);
            storeInfoTreeAddParamDTO.setRemark(dataDTO.getRemark());
            storeInfoTreeAddParamDTO.setBelong(belong);
            storeInfoTreeAddParamDTO.setOrgId(orgId);
            storeInfoTreeAddParamDTO.setOrgCode(orgCode);
            storeInfoTreeAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
            storeInfoTreeAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
            storeOfflineStoreInfoTreeService.add(storeInfoTreeAddParamDTO);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeById(Long id, String belong){

        // 校验门店是否存在
        StoreOfflineStoreInfoDetailParamDTO storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeDetailParamDTO.setId(id);
        storeDetailParamDTO.setBelong(belong);
        StoreOfflineStoreInfoDetailResp storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long storeDataId = storeInfoDetailResp.getId();
        String orgId = storeInfoDetailResp.getOrgId();
        String orgCode = storeInfoDetailResp.getOrgCode();
        String nodeCode = storeInfoDetailResp.getNodeCode();
        String storeCode = storeInfoDetailResp.getCode();

        // todo 检查是否门店已被引用
        boolean isUsedFlag = false;
        if(isUsedFlag){
            throw new DefaultException("已引用的门店，不能被删除");
        }

        // 查询门店所在节点
        StoreOfflineStoreInfoTreeDetailParamDTO relationDTO = new StoreOfflineStoreInfoTreeDetailParamDTO();
        relationDTO.setBelong(belong);
        relationDTO.setStoreCode(storeCode);
        StoreOfflineStoreInfoTreeDetailResp relationResp = storeOfflineStoreInfoTreeService.getDetail(relationDTO);
        if(relationResp != null){
            nodeCode = relationResp.getNodeCode();
        }

        // 删除门店
        StoreOfflineStoreInfoDeleteByIdParamDTO deleteByIdParamDTO = new StoreOfflineStoreInfoDeleteByIdParamDTO();
        deleteByIdParamDTO.setId(storeDataId);
        storeOfflineStoreInfoService.deleteById(deleteByIdParamDTO);

        // 删除关联关系
        storeOfflineStoreInfoTreeService.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void removeStore(OfflineStoreRemoveParamDTO dataDTO){

        dataDTO.check();

        Long id = dataDTO.getId();
        String belong = dataDTO.getBelong();
        String nodeCode = dataDTO.getNodeCode();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String storeCode = dataDTO.getStoreCode();

        // 校验节点是否合法
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode)){
            StoreOfflineStoreTreeDetailParamDTO treeDetailParamDTO = new StoreOfflineStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(nodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreOfflineStoreTreeDetailResp treeDetailResp = storeOfflineStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("节点不合法");
            }
        }

        // 校验门店是否存在
        StoreOfflineStoreInfoDetailParamDTO storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeDetailParamDTO.setId(id);
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreOfflineStoreInfoDetailResp storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long storeDataId = storeInfoDetailResp.getId();

        // 校验此门店是否在此节点下
        StoreOfflineStoreInfoTreeDetailParamDTO storeInfoTreeDetailParamDTO = new StoreOfflineStoreInfoTreeDetailParamDTO();
        storeInfoTreeDetailParamDTO.setBelong(belong);
        storeInfoTreeDetailParamDTO.setOrgId(orgId);
        storeInfoTreeDetailParamDTO.setOrgCode(orgCode);
        storeInfoTreeDetailParamDTO.setStoreCode(storeCode);
        storeInfoTreeDetailParamDTO.setNodeCode(nodeCode);
        storeInfoTreeDetailParamDTO.setDel(0);
        StoreOfflineStoreInfoTreeDetailResp storeOfflineStoreInfoTreeDetailResp = storeOfflineStoreInfoTreeService.getDetail(storeInfoTreeDetailParamDTO);
        if(storeOfflineStoreInfoTreeDetailResp == null){
            throw new DefaultException("节点下无此门店");
        }

        // todo 检查是否门店已被引用
        boolean isUsedFlag = false;
        if(isUsedFlag){
            throw new DefaultException("已引用的门店，不能被删除");
        }

        // 删除门店
        StoreOfflineStoreInfoDeleteByIdParamDTO deleteByIdParamDTO = new StoreOfflineStoreInfoDeleteByIdParamDTO();
        deleteByIdParamDTO.setId(storeDataId);
        storeOfflineStoreInfoService.deleteById(deleteByIdParamDTO);

        // 删除关联关系
        storeOfflineStoreInfoTreeService.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void updateStoreBasicInfo(OfflineStoreBasicUpdateParamDTO dataDTO){

        String belong = dataDTO.getBelong();
//        String nodeCode = dataDTO.getNodeCode();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String storeCode = dataDTO.getCode();
        String storeName = dataDTO.getName();

        // 校验门店编码是否存在
        StoreOfflineStoreInfoDetailParamDTO storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreOfflineStoreInfoDetailResp storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long id = storeInfoDetailResp.getId();

        // 更新门店信息
        StoreOfflineStoreInfoUpdateByIdParamDTO storeBasicInfoDTO = new StoreOfflineStoreInfoUpdateByIdParamDTO();
        storeBasicInfoDTO.setId(id);
        storeBasicInfoDTO.setName(storeName);
        storeBasicInfoDTO.setLogo(dataDTO.getLogo());
        storeBasicInfoDTO.setContactUserName(dataDTO.getContactUserName());
        storeBasicInfoDTO.setContactTel(dataDTO.getContactTel());
        storeBasicInfoDTO.setAddress(dataDTO.getAddress());
        storeBasicInfoDTO.setCoordinate(dataDTO.getCoordinate());
        storeBasicInfoDTO.setRemark(dataDTO.getRemark());
        storeBasicInfoDTO.setUpdateUserId(dataDTO.getUpdateUserId());
        storeBasicInfoDTO.setUpdateUserName(dataDTO.getUpdateUserName());
        storeBasicInfoDTO.setUpdateTime(new Date());
        storeOfflineStoreInfoService.updateBasicInfo(storeBasicInfoDTO);
    }

    public void updateStoreNodeInfo(OfflineStoreNodeUpdateParamDTO dataDTO){

        String storeCode = dataDTO.getCode();
        String sourceNodeCode = dataDTO.getSourceNodeCode();
        String targetNodeCode = dataDTO.getTargetNodeCode();
        String belong = dataDTO.getBelong();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();

        // 源节点和目标节点一致，则无需处理
        if(sourceNodeCode.equals(targetNodeCode)){
            return;
        }

        // 跟节点下不能有门店
        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
            throw new DefaultException(DianDianStoreConstant.TREE_NODE_NAME_TOP + "下不能关联门店");
        }

        // 校验门店编码是否存在
        StoreOfflineStoreInfoDetailParamDTO storeDetailParamDTO = new StoreOfflineStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreOfflineStoreInfoDetailResp storeInfoDetailResp = storeOfflineStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }

        // 1. 从“未分组”移动到“其他节点”时
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){

            // 校验目标节点是否合法
            if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
                StoreOfflineStoreTreeDetailParamDTO treeDetailParamDTO = new StoreOfflineStoreTreeDetailParamDTO();
                treeDetailParamDTO.setNodeCode(targetNodeCode);
                treeDetailParamDTO.setBelong(belong);
                treeDetailParamDTO.setOrgId(orgId);
                treeDetailParamDTO.setOrgCode(orgCode);
                treeDetailParamDTO.setDel(0);
                StoreOfflineStoreTreeDetailResp treeDetailResp = storeOfflineStoreTreeService.getDetail(treeDetailParamDTO);
                if(treeDetailResp == null){
                    throw new DefaultException("目标节点不合法");
                }
            }

            // 删除可能存在的关联关系，避免出现重复关联数据
            storeOfflineStoreInfoTreeService.deleteByStoreCode(belong, orgId, orgCode, storeCode);

            // 添加关联关系
            StoreOfflineStoreInfoTreeAddParamDTO addParamDTO = new StoreOfflineStoreInfoTreeAddParamDTO();
            addParamDTO.setNodeCode(targetNodeCode);
            addParamDTO.setBelong(belong);
            addParamDTO.setOrgId(orgId);
            addParamDTO.setOrgCode(orgCode);
            addParamDTO.setStoreCode(storeCode);
            addParamDTO.setCreateUserId(dataDTO.getUpdateUserId());
            addParamDTO.setCreateUserName(dataDTO.getUpdateUserName());
            storeOfflineStoreInfoTreeService.add(addParamDTO);

            return;

        }

        // 2. 如果从“其他节点”迁移到“未分组”时
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(targetNodeCode)){

            // 校验原节点是否有此门店
            Long id = null;
            if(!DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){
                StoreOfflineStoreInfoTreeDetailParamDTO infoTreeDetailParamDTO = new StoreOfflineStoreInfoTreeDetailParamDTO();
                infoTreeDetailParamDTO.setNodeCode(sourceNodeCode);
                infoTreeDetailParamDTO.setStoreCode(storeCode);
                infoTreeDetailParamDTO.setBelong(belong);
                infoTreeDetailParamDTO.setOrgId(orgId);
                infoTreeDetailParamDTO.setOrgCode(orgCode);
                infoTreeDetailParamDTO.setDel(0);
                StoreOfflineStoreInfoTreeDetailResp sourceRelationResp = storeOfflineStoreInfoTreeService.getDetail(infoTreeDetailParamDTO);
                if(sourceRelationResp == null){
                    throw new DefaultException("原节点下无此门店");
                }
                id = sourceRelationResp.getId();
            }

            // 删除关联关系
            StoreOfflineStoreInfoTreeDeleteByIdParamDTO deleteByIdParamDTO = new StoreOfflineStoreInfoTreeDeleteByIdParamDTO();
            deleteByIdParamDTO.setId(id);
            storeOfflineStoreInfoTreeService.deleteById(deleteByIdParamDTO);

            return;
        }

        // 3. 如果节点迁移，不涉及到“未分组节点”
        // 校验目标节点是否合法
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
            StoreOfflineStoreTreeDetailParamDTO treeDetailParamDTO = new StoreOfflineStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(targetNodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreOfflineStoreTreeDetailResp treeDetailResp = storeOfflineStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("目标节点不合法");
            }
        }

        // 校验原节点是否有此门店（从“未分组”移动到“其他节点”时不校验）
        Long id = null;
        if(!DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){
            StoreOfflineStoreInfoTreeDetailParamDTO infoTreeDetailParamDTO = new StoreOfflineStoreInfoTreeDetailParamDTO();
            infoTreeDetailParamDTO.setNodeCode(sourceNodeCode);
            infoTreeDetailParamDTO.setStoreCode(storeCode);
            infoTreeDetailParamDTO.setBelong(belong);
            infoTreeDetailParamDTO.setOrgId(orgId);
            infoTreeDetailParamDTO.setOrgCode(orgCode);
            infoTreeDetailParamDTO.setDel(0);
            StoreOfflineStoreInfoTreeDetailResp sourceRelationResp = storeOfflineStoreInfoTreeService.getDetail(infoTreeDetailParamDTO);
            if(sourceRelationResp == null){
                throw new DefaultException("原节点下无此门店");
            }
            id = sourceRelationResp.getId();
        }

        // 如果节点迁移，不涉及到“未分组节点”，修改门店节点关联关系
        StoreOfflineStoreInfoTreeUpdateByIdParamDTO updateDTO = new StoreOfflineStoreInfoTreeUpdateByIdParamDTO();
        updateDTO.setId(id);
        updateDTO.setNodeCode(targetNodeCode);
        updateDTO.setUpdateUserId(dataDTO.getUpdateUserId());
        updateDTO.setUpdateUserName(dataDTO.getUpdateUserName());
        updateDTO.setUpdateTime(new Date());
        storeOfflineStoreInfoTreeService.updateRelation(updateDTO);
    }
}
